// packageLongInsure/pages/reduce-insurance/reduce-insurance.js
import user from '../../../utils/user'
import {
    keyRenewal, // 恢复 （续保）
    neglectInform, // 忽略
    substractInsurance
} from '../../api/long-insure'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        testingInsureInfo: {},
        msg: '',
        autoTerminalEnsureTime: '',
        autoTerminalEnsureTimeMonth: '',
        oneKeyEnsureSucc: '',
        isExistAuto: Boolean,
        isExistAutoMonth: Boolean,
        onekeyEnsureStartDate: '',
        onekeyEnsureWorkerNum: Number,
        isSufficient: false,
        currentBalance: Number,
        isEnoughBalance: Boolean,
        oneKeyEnsureSucc: Boolean,
        tomorrowNeedCostFee: Number
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {},
    onShow() {
        user.waitLogin().then(() => {
            this.substractInsurance()
        })
    },
    substractInsurance() {
        substractInsurance().then((res) => {
            let isExistAuto = res.isExistAutoTerminalEnsure
            let isExistAutoMonth = res.isExistAutoTerminalEnsureMonth
            this.setData({
                isExistAuto: isExistAuto,
                isExistAutoMonth: isExistAutoMonth,
                autoTerminalEnsureTime: res.autoTerminalEnsureTime,
                autoTerminalEnsureTimeMonth: res.autoTerminalEnsureMonthTime
            })
        })
    },
    async onKeyRenewal(e) {
        let type = e.currentTarget.dataset.type
        const result = await app.confirm('所有系统减保人员信息/方案保持不变并立即加保')
        const res = await keyRenewal({
            type: type
        })
        console.log(res, 'resres')
        this.setData({
            msg: res.msg,
            testingInsureInfo: res,
            oneKeyEnsureSucc: res.oneKeyEnsureSucc,
            onekeyEnsureStartDate: res.onekeyEnsureStartDate,
            onekeyEnsureWorkerNum: res.onekeyEnsureWorkerNum,
            currentBalance: res.currentBalance,
            isEnoughBalance: res.isEnoughBalance,
            tomorrowNeedCostFee: res.tomorrowNeedCostFee
        })
        if (!res.oneKeyEnsureSucc) {
            if (res.isEnoughBalance !== undefined && !res.isEnoughBalance) {
                this.setData({
                    isSufficient: true
                })
            } else {
                app.alert(res.msg)
            }
            return
        }
        app.toast(this.data.msg)
        setTimeout(() => {
            wx.redirectTo({
                url: '/pages/success/success?title=续保成功&content=本次续保共' + this.data.onekeyEnsureWorkerNum + '人，生效日期为' + this.data.onekeyEnsureStartDate,
            })
        }, 1000)
    },
    neglectInform(e) {
        let type = e.currentTarget.dataset.type
        app.confirm('忽略操作将清空本列表如需投保您可在方案中重新发起加保操作').then(() => {
            neglectInform({
                type: type
            }).then((res) => {
                wx.navigateBack()
            })
        })
    },
    onSufficient() {
        this.setData({
            isSufficient: false
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})